package com.ruoyi.system.service.impl.video;

import java.util.List;

import com.ruoyi.common.core.domain.model.VsUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.video.VsDramasVideo;
import com.ruoyi.system.mapper.video.VsDramasVideoMapper;
import com.ruoyi.system.service.video.IVsDramasVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.video.VsDramasCategoryMapper;
import com.ruoyi.system.domain.video.VsDramasCategory;
import com.ruoyi.system.service.video.IVsDramasCategoryService;

/**
 * 商城分类Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-04-19
 */
@Service
public class VsDramasCategoryServiceImpl implements IVsDramasCategoryService 
{
    @Autowired
    private VsDramasCategoryMapper vsDramasCategoryMapper;
    @Autowired
    private IVsDramasVideoService videoMapper;

    /**
     * 查询商城分类
     * 
     * @param id 商城分类主键
     * @return 商城分类
     */
    @Override
    public VsDramasCategory selectVsDramasCategoryById(Integer id)
    {
        return vsDramasCategoryMapper.selectVsDramasCategoryById(id);
    }

    /**
     * 查询商城父分类列表
     * 
     * @param vsDramasCategory 商城分类
     * @return 商城分类
     */
    @Override
    public List<VsDramasCategory> selectFatherCategoryList(VsDramasCategory vsDramasCategory)
    {
        return vsDramasCategoryMapper.selectFatherCategoryList(vsDramasCategory);
    }

    /**
     * 查询商城子分类列表
     *
     * @param vsDramasCategory 商城分类
     * @return 商城分类
     */
    @Override
    public List<VsDramasCategory> selectChildrenCategoryList(VsDramasCategory vsDramasCategory)
    {
        return vsDramasCategoryMapper.selectChildrenCategoryList(vsDramasCategory);
    }

    /**
     * 新增商城分类
     * 
     * @param vsDramasCategory 商城分类
     * @return 结果
     */
    @Override
    public int insertVsDramasCategory(VsDramasCategory vsDramasCategory)
    {
        vsDramasCategory.setCreatetime(System.currentTimeMillis());
        vsDramasCategory.setUpdatetime(System.currentTimeMillis());
        return vsDramasCategoryMapper.insertVsDramasCategory(vsDramasCategory);
    }

    /**
     * 修改商城分类
     * 
     * @param vsDramasCategory 商城分类
     * @return 结果
     */
    @Override
    public int updateVsDramasCategory(VsDramasCategory vsDramasCategory)
    {
        vsDramasCategory.setUpdatetime(System.currentTimeMillis());
        return vsDramasCategoryMapper.updateVsDramasCategory(vsDramasCategory);
    }

    /**
     * 批量删除商城分类
     * 
     * @param ids 需要删除的商城分类主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasCategoryByIds(Integer[] ids)
    {
        return vsDramasCategoryMapper.deleteVsDramasCategoryByIds(ids);
    }

    /**
     * 删除商城分类信息
     * 
     * @param id 商城分类主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasCategoryById(Integer id)
    {
        return vsDramasCategoryMapper.deleteVsDramasCategoryById(id);
    }

    /**
     * 批量修改商城分类
     *
     * @param vsDramasCategory 商城分类
     * @return 结果
     */
    @Override
    public int batchUpdateVsDramasCategory(List<VsDramasCategory> vsDramasCategory) {
        return vsDramasCategoryMapper.batchUpdateVsDramasCategory(vsDramasCategory);
    }

    @Override
    public List<VsDramasCategory> selectHomeVideos(VsDramasCategory vsDramasCategory) {
        List<VsDramasCategory> list = vsDramasCategoryMapper.selectChildrenCategoryList(vsDramasCategory);
        VsUser user = null;
        try {
            user = SecurityUtils.getLoginUser().getVsUser();
        } catch (Exception e) {
            // 因为进入主页用户可能不是登录状态，所以捕获一下异常防止不往下走
            System.out.println(e);
        }
        for (VsDramasCategory v: list) {
            VsDramasVideo video = new VsDramasVideo();
            video.setCategoryIds(String.valueOf(v.getId()));
            video.setLangId(v.getLangId());
            video.setLimit(8);
            if (StringUtils.isNull(user)) {
                video.setNoUser(1);
            }
            List<VsDramasVideo> videos = videoMapper.selectVsDramasVideoList(video);
            v.setVideos(videos);
        }
        return list;
    }
}
